package DAO;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Subject;
import util.DataUtil;

public class SubjectDAO {

    public static List<Subject> getAll() {
        List<Subject> list = new ArrayList<Subject>();
        String call = "{Call GET_ALL_SUBJECT}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                Subject sbj = new Subject();
                sbj.setSubjectId(rs.getString("SubjectID"));
                sbj.setSubjectName(rs.getString("SubName"));
                sbj.setNoSession(rs.getInt("NoOfSession"));
                sbj.setDescription(rs.getString("Description"));
                list.add(sbj);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static boolean checkSubjectID(String id) {
        boolean fl = true;
        String call = "{Call CHECK_SUBJECT_ID(?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, id);
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                fl = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fl;
    }

    public static boolean checkSubjectName(String name) {
        boolean fl = true;
        String call = "{Call CHECK_SUBJECT_NAME(?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, name);
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                fl = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fl;
    }

    public static boolean addSubject(Subject sbj) {
        int rs = 0;
        String call = "{Call ADD_SUBJECT(?,?,?,?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, sbj.getSubjectId());
            cs.setString(2, sbj.getSubjectName());
            cs.setInt(3, sbj.getNoSession());
            cs.setString(4, sbj.getDescription());
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (rs > 0) ? true : false;
    }

    public static boolean editSubject(Subject sbj) {
        int rs = 0;
        String call = "{Call EDIT_SUBJECT(?,?,?,?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, sbj.getSubjectId());
            cs.setString(2, sbj.getSubjectName());
            cs.setInt(3, sbj.getNoSession());
            cs.setString(4, sbj.getDescription());
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (rs > 0) ? true : false;
    }
    public static boolean deleteSubject(String id){
        int rs = 0;
        String call = "{Call DETELE_SUBJECT(?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, id);
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (rs > 0) ? true : false;
    }
    
    
    public static List<Subject> search(String filter,String key,List<Subject> listSubject){
        List<Subject> list = new ArrayList<Subject>();
        for(Subject sbj:listSubject){
            if(filter.equals("Subject ID")){
                if((sbj.getSubjectId().toLowerCase()).equals(key.toLowerCase())){
                    list.add(sbj);
                }
            }
            if(filter.equals("Subject Name")){
                if((sbj.getSubjectName().toLowerCase()).equals(key.toLowerCase())){
                    list.add(sbj);
                }
            }
            if(filter.equals("No Of Session")){
                if(((sbj.getNoSession()+"").toLowerCase()).equals(key.toLowerCase())){
                    list.add(sbj);
                }
            }
        }
        return list;
    }
}
